package com.ship.contract.bean;

import cn.hutool.core.date.DatePattern;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ship.common.core.base.ContractBaseBean;
import com.ship.common.core.base.Joinable;
import com.ship.common.core.mapping.Label;
import com.ship.common.core.mapping.SearchItem;
import com.ship.common.core.mapping.TableColumn;
import com.ship.common.core.validate.Add;
import com.ship.common.core.validate.Update;
import com.ship.common.security.utils.SecurityUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;


import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

/**
 * @author 31463
 */
@TableName("con_shipping_agency")
@Data
public class ConShippingAgency extends ContractBaseBean implements Joinable {

    private static final long serialVersionUID = 3601481852571027621L;
    /**
     * 执行编号
     */
    @Label(value = "执行编号")
    @SearchItem
    @TableColumn
    private String contractNumber;

    /**
     * 合同名称
     */
    @Label(value = "合同名称")
    @SearchItem
    @TableColumn
    private String contractName;

    /**
     * 贸易主体(国贸、新贸)
     */
    @Label(value = "合同主体", enumContent = "贸易主体", multiSelect = true)
    @SearchItem
//    @TableColumn
//    @NotNull(message = "合同主体不能为空", groups = {Add.class})
    private Integer tradeBody;

    @Label(value = "合同主体", enumContent = "贸易主体", multiSelect = true)
    @TableColumn
    @TableField(exist = false)
    private String tradeBodyStr;

    @TableField(exist = false)
    private String tradeBodyListForSearch;

    /**
     * 合同类型（主供应商、备供应商）
     */
    @Label(value = "合同类型", enumContent = "代理合同类型", multiSelect = true)
    @SearchItem
//    @TableColumn
    @NotNull(message = "合同类型不能为空", groups = {Add.class})
    private Integer contractType;

    @Label(value = "合同类型", enumContent = "代理合同类型", multiSelect = true)
    @TableColumn
    @TableField(exist = false)
    private String contractTypeStr;

    @TableField(exist = false)
    private String contractTypeListForSearch;

    /**
     * 甲方
     */
    @Label(value = "甲方")
    @SearchItem
    @TableColumn
    @NotBlank(message = "甲方不能为空", groups = {Update.class, Add.class})
    @Size(max = 255, message = "甲方长度不能超过255", groups = {Update.class, Add.class})
    private String client;

    /**
     * 乙方
     */
    @Label(value = "乙方")
    @SearchItem
    @TableColumn
    @NotBlank(message = "乙方不能为空", groups = {Update.class, Add.class})
    @Size(max = 255, message = "乙方长度不能超过255", groups = {Update.class, Add.class})
    private String companyName;

    /**
     * 签订日期
     */
    @Label(value = "签订日期")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @SearchItem
//    @TableColumn
    @NotNull(message = "签订日期不能为空", groups = {Add.class})
    private Date signingDate;

    @Label(value = "签订日期")
    @TableColumn
    @TableField(exist = false)
    private String signingDateStr;

    @ApiModelProperty(value = "生效日期")
    @Label(value = "合同生效日期")
    @SearchItem
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
//    @TableColumn
    @NotNull(message = "签订日期不能为空", groups = {Add.class})
    private Date comeIntoForceDate;

    @Label(value = "合同生效日期")
    @TableColumn
    @TableField(exist = false)
    private String comeIntoForceDateStr;

    /**
     * 合同有效期（单位年）
     */
    @Label(value = "合同有效期")
    @NotNull(message = "合同有效期不能为空", groups = {Add.class})
    private BigDecimal contractValidity;

    /**
     * 合同有效期（单位年）
     */
    @Label(value = "合同有效期")
    @TableField(exist = false)
    @TableColumn
    private String contractValidityStr;

    /**
     * 合同到期日期
     */
    @Label(value = "合同到期日期")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @SearchItem
//    @TableColumn
    private Date expirationDate;

    @Label(value = "合同到期日期")
    @TableColumn
    @TableField(exist = false)
    private String expirationDateStr;

    @ApiModelProperty(value = "预付款时间文本")
    @Label(value = "预付款时间文本")
    @NotNull(message = "预付款时间文本不能为空", groups = {Add.class})
    private String advancePayDaysText;

    /**
     * 预付款时间（单位天）
     */
    @Label(value = "预付款时间")
    @NotNull(message = "预付款时间不能为空", groups = {Add.class})
    private Integer advancePayDays;

    /**
     * 预付款时间（单位天）
     */
    @Label(value = "预付款时间")
    @TableField(exist = false)
    @TableColumn
    private String advancePayDaysStr;

    /**
     * 预付款时间类型(工作日，自然日)
     */
    @Label(value = "预付款时间类型", enumContent = "预付款时间类型")
    private Integer advancePayDayType;

    @ApiModelProperty(value = "船代发送最终发票时间文本")
    @Label(value = "船代发送最终发票时间文本")
    @NotNull(message = "船代发送最终发票时间文本不能为空", groups = {Add.class})
    private String sendInvoiceLastDaysText;

    /**
     * 船代发送最终发票时间（天）
     */
    @Label(value = "船代发送最终发票时间")
    @NotNull(message = "船代发送最终发票时间不能为空", groups = {Add.class})
    private Integer sendInvoiceLastDays;

    /**
     * 船代发送最终发票时间（天）
     */
    @Label(value = "船代发送最终发票时间")
    @TableField(exist = false)
    @TableColumn
    private String sendInvoiceLastDaysStr;

    /**
     * 船代发送最终发票时间类型(工作日，自然日)
     */
    @Label(value = "船代发送最终发票时间类型", enumContent = "船代发送最终发票时间类型")
    private Integer sendInvoiceLastDaysType;

    @ApiModelProperty(value = "支付时间文本")
    @Label(value = "支付时间文本")
    @NotNull(message = "支付时间文本不能为空", groups = {Add.class})
    private String payDaysText;

    /**
     * 支付时间（天）
     */
    @Label(value = "支付时间")
    @NotNull(message = "支付时间不能为空", groups = {Add.class})
    private Integer payDays;

    /**
     *支付时间（天）
     */
//    @Label(value="结算时间")
//    @TableField(exist = false)
//    @TableColumn
//    private String payDaysStr;

    /**
     * 支付时间类型(工作日，自然日)
     */
    @Label(value = "支付时间类型", enumContent = "支付时间类型")
    private Integer payDaysType;

    /**
     * 委托人银行名称
     */
    @Label(value = "委托人银行名称")
    @TableColumn
    private String bankNameClient;

    /**
     * 委托人银行地址
     */
    @Label(value = "委托人银行地址")
    @TableColumn
    private String bankAddrClient;

    /**
     * 委托人账户号码
     */
    @Label(value = "委托人账户号码")
    @TableColumn
    private String bankAccountNumClient;

    /**
     * 委托人账户名称
     */
    @Label(value = "委托人账户名称")
    @TableColumn
    private String bankAccountNameClient;

    /**
     * 委托人IBAN
     */
    @Label(value = "委托人IBAN")
    @TableColumn
    private String ibanClient;

    /**
     * 委托人SWIFT CODE
     */
    @Label(value = "委托人SWIFT CODE")
    @TableColumn
    private String swiftCodeClient;

    /**
     * 委托人币种
     */
    @Label(value = "委托人币种")
//    @TableColumn
    private String currencyClient;

    @Label(value = "委托人币种")
    @TableColumn
    @TableField(exist = false)
    private String currencyClientStr;

    /**
     * 委托人关联银行名称
     */
    @Label(value = "委托人关联银行名称")
    @TableColumn
    private String relateBankNameClient;

    /**
     * 委托人关联银行SWIFT CODE
     */
    @Label(value = "委托人关联银行SWIFT CODE")
    @TableColumn
    private String relateBankSwiftCodeClient;

    /**
     * 船代银行名称
     */
    @Label(value = "船代银行名称")
    @TableColumn
    private String bankNameShipAgency;

    /**
     * 船代银行地址
     */
    @Label(value = "船代银行地址")
    @TableColumn
    private String bankAddrShipAgency;

    /**
     * 船代账户号码
     */
    @Label(value = "船代账户号码")
    @TableColumn
    private String bankAccountNumShipAgency;

    /**
     * 船代账户名称
     */
    @Label(value = "船代账户名称")
    @TableColumn
    private String bankAccountNameShipAgency;

    /**
     * 船代IBAN
     */
    @Label(value = "船代IBAN")
    @TableColumn
    private String ibanShipAgency;

    /**
     * 船代SWIFT CODE
     */
    @Label(value = "船代SWIFT CODE")
    @TableColumn
    private String swiftCodeShipAgency;

    /**
     * 船代币种
     */
    @Label(value = "船代币种")
//    @TableColumn
    private String currencyShipAgency;

    @Label(value = "船代币种")
    @TableColumn
    @TableField(exist = false)
    private String currencyShipAgencyStr;

    /**
     * 委托人联系人
     */
    @Label(value = "委托人联系人")
    @TableColumn
    private String contactsClient;

    /**
     * 委托人邮箱
     */
    @Label(value = "委托人邮箱")
    @TableColumn
    private String mailboxClient;

    /**
     * 委托人抄送人
     */
    @Label(value = "委托人抄送人")
    @TableColumn
    private String ccClient;

    /**
     * 委托人抄送人邮箱
     */
    @Label(value = "委托人抄送人邮箱")
    @TableColumn
    private String ccMailboxClient;

    /**
     * 船代联系人
     */
    @Label(value = "船代联系人")
    @TableColumn
    private String contactsShipAgency;

    /**
     * 船代邮箱
     */
    @Label(value = "船代邮箱")
    @TableColumn
    private String mailboxShipAgency;

    /**
     * 船代抄送人
     */
    @Label(value = "船代抄送人")
    @TableColumn
    private String ccShipAgency;

    /**
     * 船代抄送人邮箱
     */
    @Label(value = "船代抄送人邮箱")
    @TableColumn
    private String ccMailboxShipAgency;

    /**
     * 删除申请
     */
    @Label(value = "删除申请")
    private Integer delApply;

    /**
     * 合同状态
     */
    @Label(value = "合同状态", enumContent = "船代合同状态")
    @SearchItem
//    @TableColumn
    private Integer status;

    @Label(value = "合同状态", enumContent = "船代合同状态")
    @TableColumn
    @TableField(exist = false)
    private String statusStr;

    /**
     * 优秀案例
     */
    @Label(value = "优秀案例", enumContent = "是否是优秀案例")
    private Integer excellent;

    /**
     * 附件地址
     */
    @TableField(exist = false)
    private String filePath;

    /**
     * 合同上传时间
     */
    @Label(value = "合同上传时间")
//    @TableColumn
    @TableField(fill = FieldFill.INSERT)
    private Date createDate;

    @Label(value = "合同上传时间")
    @TableField(exist = false)
    @TableColumn
    private String createDateStr;

    /**
     * 创建者
     */
    @Label(value = "创建者")
    @TableColumn
    @TableField(fill = FieldFill.INSERT)
    private String createBy;
    @Label(value = "删除流程")
    private String deleteProcessInstanceId;


    public String getContractValidityStr() {
        String result = null;
        if (getContractValidity() != null) {
            result = numericFormatting(getContractValidity()) + "年";
        }
        return result;
    }

    public String getAdvancePayDaysStr() {
        String result = null;
        if (getAdvancePayDays() != null) {
            if (getAdvancePayDayType() == 1) {
                if (StringUtils.isNotBlank(getAdvancePayDaysText())) {
                    result = getAdvancePayDaysText() + getAdvancePayDays() + "工作日";
                } else {
                    result = getAdvancePayDays() + "工作日";
                }
            } else if (getAdvancePayDayType() == 2) {
                if (StringUtils.isNotBlank(getAdvancePayDaysText())) {
                    result = getAdvancePayDaysText() + getAdvancePayDays() + "自然日";
                } else {
                    result = getAdvancePayDays() + "自然日";
                }
            }
        }
        return result;
    }

    public String getSendInvoiceLastDaysStr() {
        String result = null;
        if (getSendInvoiceLastDays() != null) {
            if (getSendInvoiceLastDaysType() == 1) {
                if (StringUtils.isNotBlank(getSendInvoiceLastDaysText())) {
                    result = getSendInvoiceLastDaysText() + getSendInvoiceLastDays() + "工作日";
                } else {
                    result = getSendInvoiceLastDays() + "工作日";
                }
            } else if (getSendInvoiceLastDaysType() == 2) {
                if (StringUtils.isNotBlank(getSendInvoiceLastDaysText())) {
                    result = getSendInvoiceLastDaysText() + getSendInvoiceLastDays() + "自然日";
                } else {
                    result = getSendInvoiceLastDays() + "自然日";
                }
            }
        }
        return result;
    }

    public String getPayDaysStr() {
        String result = null;
        if (getPayDays() != null) {
            if (getPayDaysType() == 1) {
                if (StringUtils.isNotBlank(getPayDaysText())) {
                    result = getPayDaysText() + getPayDaysType() + "工作日";
                } else {
                    result = getPayDaysType() + "工作日";
                }
            } else if (getPayDaysType() == 2) {
                if (StringUtils.isNotBlank(getPayDaysText())) {
                    result = getPayDaysText() + getPayDaysType() + "自然日";
                } else {
                    result = getPayDaysType() + "自然日";
                }
            }
        }
        return result;
    }

    public String getTradeBodyStr(){
        String result = null;
        if (getTradeBody()!=null){
            if (tradeBody == 1){
                result = "国贸";
            }
            if (tradeBody == 2){
                result = "新贸";
            }
        }
        return result;
    }

    public String getContractTypeStr(){
        String result = null;
        if (getContractType()!=null){
            if (contractType == 1){
                result = "主供应商";
            }
            if (contractType == 2){
                result = "备供应商";
            }
        }
        return result;
    }

    public String getSigningDateStr() {
        String SigningDateStr = null;
        if (null != this.getSigningDate()){
            SigningDateStr = cn.hutool.core.date.DateUtil.format(this.getSigningDate(), DatePattern.NORM_DATE_FORMATTER);
        }
        return SigningDateStr;
    }

    public String getComeIntoForceDateStr() {
        String ComeIntoForceDateStr = null;
        if (null != this.getComeIntoForceDate()){
            ComeIntoForceDateStr = cn.hutool.core.date.DateUtil.format(this.getComeIntoForceDate(), DatePattern.NORM_DATE_FORMATTER);
        }
        return ComeIntoForceDateStr;
    }

    public String getExpirationDateStr() {
        String ExpirationDateStr = null;
        if (null != this.getExpirationDate()){
            ExpirationDateStr = cn.hutool.core.date.DateUtil.format(this.getExpirationDate(), DatePattern.NORM_DATE_FORMATTER);
        }
        return ExpirationDateStr;
    }

    public String getCurrencyClientStr() {
//        if(SecurityUtils.getLoginUser() != null && !SecurityUtils.isAdmin(SecurityUtils.getUserId())
//                && SecurityUtils.getLoginUser().getPermissionString().indexOf("spcon:price:show") < 0){
//            return "***";
//        }
        String result=null;
        if (getCurrencyClient()!=null) {
//            result = decimalFormat.format(getCurrencyClient());
            if (Objects.equals(currencyClient, "1")){
                result = getCurrencyClient()+"美元USD";
            }
            if (Objects.equals(currencyClient, "2")){
                result = getCurrencyClient()+"人民币CNY";
            }
            if (Objects.equals(currencyClient, "3")){
                result = getCurrencyClient()+"欧元EUR";
            }
        }
        return result;
    }

    public String getCurrencyShipAgencyStr() {
//        if(SecurityUtils.getLoginUser() != null && !SecurityUtils.isAdmin(SecurityUtils.getUserId())
//                && SecurityUtils.getLoginUser().getPermissionString().indexOf("spcon:price:show") < 0){
//            return "***";
//        }
        String result=null;
        if (getCurrencyShipAgency()!=null) {
//            result = decimalFormat.format(getCurrencyShipAgency());
            if (Objects.equals(currencyClient, "1")){
                result = getCurrencyShipAgency()+"美元USD";
            }
            if (Objects.equals(currencyClient, "2")){
                result = getCurrencyShipAgency()+"人民币CNY";
            }
            if (Objects.equals(currencyClient, "3")){
                result = getCurrencyShipAgency()+"欧元EUR";
            }
        }
        return result;
    }

    public String getStatusStr(){
        String result = null;
        if (getStatus()!=null){
            if (status == 1){
                result = "草稿";
            }
            if (status == 2){
                result = "已签订";
            }
            if (status == 3){
                result = "执行中";
            }
            if (status == 4){
                result = "已完成";
            }
            if (status == 5){
                result = "off-hire";
            }
        }
        return result;
    }

    public String getCreateDateStr() {
        String createDateStr = null;
        if (null != this.getCreateDate()){
            createDateStr = cn.hutool.core.date.DateUtil.format(this.getCreateDate(), DatePattern.NORM_DATETIME_FORMATTER);
        }
        return createDateStr;
    }

    @Override
    public String getLabel() {
        return null;
    }

    @Override
    public String getValue() {
        return null;
    }
}
